﻿using Microsoft.UI.Xaml.Data;

namespace DimensionsHelper.Client.WinUi.Helpers;

public partial class EnumDescriptionConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, string language) =>
        ConvertValue(value, targetType);

    public object ConvertBack(object value, Type targetType, object parameter, string language) =>
        ConvertValue(value, targetType);

    private static object ConvertValue(object value, Type targetType)
    {
        if (targetType.IsEnum)
        {
            if (value is string desc)
            {
                return Enum.ToObject(targetType, EnumDescriptionHelper.GetValueOfDescription(targetType, desc));
            }

            return Enum.ToObject(targetType, 0);
        }

        Type valueType = value.GetType();
        return EnumDescriptionHelper.GetDescriptionOfValue(valueType, (int)value);
    }
}